@echo off
echo ==========================================
echo 营养系统菜品处理进度监控
echo ==========================================

:loop
cls
echo 当前时间: %date% %time%
echo.

echo === 任务状态 ===
schtasks /query /tn "NutritionSystemDishProcessing" /fo LIST | findstr "上次运行时间 下次运行时间 状态"

echo.
echo === 数据库处理进度 ===
cd /d D:\wwwroot\yingyang
php -r "
<?php
try {
    \$config = ['hostname'=>'127.0.0.1', 'database'=>'yingyang', 'username'=>'root', 'password'=>'root', 'charset'=>'utf8mb4'];
    \$dsn = 'mysql:host=' . \$config['hostname'] . ';dbname=' . \$config['database'] . ';charset=' . \$config['charset'];
    \$pdo = new PDO(\$dsn, \$config['username'], \$config['password']);
    
    \$stmt = \$pdo->query('SELECT COUNT(*) as total FROM fa_food_dish');
    \$total = \$stmt->fetch(PDO::FETCH_ASSOC);
    
    \$stmt = \$pdo->query('SELECT COUNT(*) as processed FROM fa_food_dish WHERE ai_called = 1');
    \$processed = \$stmt->fetch(PDO::FETCH_ASSOC);
    
    \$stmt = \$pdo->query('SELECT COUNT(*) as unprocessed FROM fa_food_dish WHERE ai_called = 0');
    \$unprocessed = \$stmt->fetch(PDO::FETCH_ASSOC);
    
    echo '总菜品数: ' . \$total['total'] . PHP_EOL;
    echo '已处理数: ' . \$processed['processed'] . PHP_EOL;
    echo '未处理数: ' . \$unprocessed['unprocessed'] . PHP_EOL;
    
    if (\$total['total'] > 0) {
        \$progress = round((\$processed['processed'] / \$total['total']) * 100, 2);
        echo '处理进度: ' . \$progress . '%%' . PHP_EOL;
    }
} catch (Exception \$e) {
    echo '查询失败: ' . \$e->getMessage() . PHP_EOL;
}
?>
"

echo.
echo === 最新处理记录 ===
php -r "
<?php
try {
    \$config = ['hostname'=>'127.0.0.1', 'database'=>'yingyang', 'username'=>'root', 'password'=>'root', 'charset'=>'utf8mb4'];
    \$dsn = 'mysql:host=' . \$config['hostname'] . ';dbname=' . \$config['database'] . ';charset=' . \$config['charset'];
    \$pdo = new PDO(\$dsn, \$config['username'], \$config['password']);
    
    \$stmt = \$pdo->query('SELECT id, name, cook_time FROM fa_food_dish WHERE ai_called = 1 ORDER BY updatetime DESC LIMIT 5');
    \$dishes = \$stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo '最近处理的5个菜品:' . PHP_EOL;
    foreach (\$dishes as \$dish) {
        echo '  ID: ' . \$dish['id'] . ', 名称: ' . \$dish['name'] . ', 烹饪时间: ' . \$dish['cook_time'] . PHP_EOL;
    }
} catch (Exception \$e) {
    echo '查询失败: ' . \$e->getMessage() . PHP_EOL;
}
?>
"

echo.
echo 等待30秒后刷新... (按Ctrl+C退出)
timeout /t 30 /nobreak >nul
goto loop